package com.my.screenadapter.fragmenttest;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.util.Log;

import com.my.screenadapter.R;

import java.util.List;

/**
 * Created by 张英杰 on 2017/1/11.
 */
public class FragmentContainerActivity extends FragmentActivity {

    private FragmentTransaction transaction;
    private FragmentManager manager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        init();
    }

    private void init() {
        OneFragment mOneFragment = new OneFragment();
        final TwoFragment twoFragment = new TwoFragment();
        mOneFragment.getFragmentOneData(new IFragmentData() {
            @Override
            public void passData(String data) {
                twoFragment.setData(data);
            }
        });
        addFragment(mOneFragment, "oneF");
        transaction.add(R.id.layoutcontainer2,twoFragment,"twoF");
    }

    private void addFragment(Fragment f, String tag) {

        transaction.replace(R.id.layoutcontainer1, f);
//        在transaction.commit()之前，使用addToBackStack()将其添加到回退栈中。
        transaction.addToBackStack(tag);
        transaction.commit();
        getBackStackFragment();
    }

    private void getBackStackFragment() {
        List<Fragment> mlist = manager.getFragments();
        Log.i("f===", (mlist != null ? mlist.size() : 0) + "");
        for (int i = 0; mlist != null && i < mlist.size(); i++) {
            Fragment fragment = mlist.get(i);
            if (fragment != null) {
                Log.i("f===", "tag===" +fragment.getTag()
                        + " ---  " + mlist.get(i).getClass().equals(OneFragment.class)
                );
            }
        }
        int count = manager.getBackStackEntryCount();
        Log.i("f===", count + "");
        Fragment a1 = manager.findFragmentByTag("a");
        if (a1 != null) {
            Log.i("f===", a1.getClass().getSimpleName() + "");
        }
//        Log.i("f===", (a1!=null?a1.getClass().getSimpleName():"000")+"");
        Fragment fragmentById = manager.findFragmentById(R.id.layoutcontainer1);
        Log.i("f===", (fragmentById != null ? fragmentById.getClass().getSimpleName() : "111") + "");
    }

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    Log.i("f===", "sleep--------");
                    getBackStackFragment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
